<?php

namespace app\engine\controller;

use cmf\controller\HomeBaseController;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;

class SupplierController extends HomeBaseController
{
    /**
     *
     * @return mixed|string
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getSupplier()
    {
        $data = Db::name('supplier')->select()->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function add()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5){
        //     $this->error('无权限操作');
        // }
        return $this->fetch();
    }

    public function doadd()
    {
        $data = $this->request->param();
        $kps = [];
        if (empty($data['attrName'])) {
            $this->error('公司简称为必填');
        }

        $checkattr = Db::name('supplier')->where(['attrName' => $data['attrName']])->find();
        if ($checkattr) {
            $this->error('该公司简称已存在');
        }
        if (!empty($data['name'])) {
            $checkName = Db::name('supplier')->where(['name' => $data['name']])->find();
            if ($checkName) {
                $this->error('该姓名已经存在');
            }
        }
        if (!empty($data['mobile'])) {
            $checkmobile = Db::name('supplier')->where(['mobile' => $data['mobile']])->find();
            if ($checkmobile) {
                $this->error('该联系方式已经存在');
            }
        }
        $files = $this->request->file('img');
        unset($data['img']);
        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['img'] = implode(',', $kps);
        }
        $res = Db::name('supplier')->insert($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function see()
    {
        $data = $this->request->param();
        $res = Db::name('supplier')->where('id', $data['id'])->find();
        $res['img'] = explode(',', $res['img']);
        foreach ($res['img'] as $k => &$v) {
            $v = 'http://' . $_SERVER['SERVER_NAME'] . $v;
        }
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function doedit()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5){
        //     $this->error('无权限操作');
        // }
        $data = $this->request->param();
        $id = $data['id'];
        unset($data['id']);
        $kps = [];

        $files = $this->request->file('img');
        unset($data['img']);
        $check = Db::name('supplier')->where(['id' => $id])->find();
        $img = explode(',', $check['img']);
        //            dump($img);die;

        if ($files) {
            foreach ($files as $k => $file) {
                // 移动到框架应用根目录/public/uploads/ 目录下
                $info = $file->move(CMF_ROOT . 'public' . '/' . 'uploads');
                if ($info) {
                    $kps[$k] = '/uploads/' . date('Ymd', time()) . '/' . $info->getFilename();
                } else {
                    $this->error($file->getError());
                }
            }
            $data['img'] = implode(',', $kps);
            if ($check) {
                $img = explode(',', $check['img']);
                foreach ($img as $k => $v) {
                    $v = CMF_ROOT . $v;
                    if (file_exists($v) && $v != CMF_ROOT) {
                        unlink($v);
                    }
                }
            }
        }
        $res = Db::name('supplier')->where('id', $id)->update($data);
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function del()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5 || $role == 3){
        //     $this->error('无权限操作');
        // }
        $data = $this->request->param();
        $check = Db::name('supplier')->where(['id' => $data['id']])->find();
        if ($check['img']) {
            $img = explode(',', $check['img']);
            foreach ($img as $k => $v) {
                $v = CMF_ROOT . $v;
                if (file_exists($v)) {
                    unlink($v);
                }
            }
        }
        $res = Db::name('supplier')->where('id', $data['id'])->delete();
        if (!$res) {
            $this->error('操作失败');
        }
        $this->success('操作成功');
    }

    public function able()
    {
        // $role = Db::name('role_user')->where(['user_id'=>cmf_get_current_admin_id()])->value('role_id');
        // if($role == 2 || $role == 5 ){
        //     $this->error('无权限操作');
        // }
        $data = $this->request->param();
        $check  = Db::name('supplier')->where(['id' => $data['id']])->value('able');
        $able = [];
        if ($check == 1) {
            $able['able'] = 2;
        } else {
            $able['able'] = 1;
        }
        Db::name('supplier')->where(['id' => $data['id']])->update($able);
        $this->success('操作成功');
    }
}
